//
//  ResultScene.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/6/13.
//
//

#include "ResultScene.h"
#include "SceneManager.h"
#include "GameLayerManager.h"
#include "MyUtilities.h"
#include "StaticVariables.h"
CCScene* ResultScene::scene(){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    ResultScene *layer = ResultScene::create();
    GameLayerManager::sharedInstance()->resultLayer = layer;
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}


bool ResultScene::init(){
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    double buttonWidth = winSize.width * 0.6f;
    double buttonHeight = buttonWidth/4;
    tryAgainButton = MyMenuButton::create("Try again", (winSize.width-buttonWidth)/2, winSize.height/2-buttonHeight*2, buttonWidth, buttonHeight);
    CCCallFuncO* tryAgainCallBack = CCCallFuncO::create(this, callfuncO_selector(ResultScene::onTapTryAgain), tryAgainButton);
    tryAgainButton->addCallback(tryAgainCallBack);
    this->addChild(tryAgainButton);
    
    mainMenuButton = MyMenuButton::create("Back to menu", (winSize.width-buttonWidth)/2, winSize.height/2-buttonHeight*3.5f, buttonWidth, buttonHeight);
    CCCallFuncO* mainMenuCallback = CCCallFuncO::create(this, callfuncO_selector(ResultScene::onTapMainMenu), mainMenuButton);
    mainMenuButton->addCallback(mainMenuCallback);
    this->addChild(mainMenuButton);
    
    
    scoreLabel = CCLabelBMFont::create("0", "Info.fnt");
    scoreLabel->setAnchorPoint(ccp(0.5,0.5));
    scoreLabel->setPosition(ccp(winSize.width/2,(winSize.height/2)*1.25f));
    scoreLabel->setScale(winSize.width*0.3f/scoreLabel->getContentSize().height);
    this->addChild(scoreLabel);
    
    
    setTouchEnabled(true);
    return true;
}

void ResultScene::getScoreToDisplayResult(int score){
    scoreLabel->setString(MyUtilities::toString(score));
}

void ResultScene::onTapTryAgain(cocos2d::CCObject *sender){
    CCTransitionFlipX* transition = CCTransitionFlipX::create(0.5f, SceneManager::sharedInstance()->previousScene, kCCTransitionOrientationRightOver);
    CCDirector::sharedDirector()->replaceScene(transition);

}
void ResultScene::onTapMainMenu(cocos2d::CCObject *sender){
    CCTransitionFade* transition = CCTransitionFade::create(TRANSITION_DURATION, SceneManager::sharedInstance()->quickFingerMenuScene, ccc3(0, 0, 0));
    CCDirector::sharedDirector()->replaceScene(transition);
}

void ResultScene::ccTouchesBegan(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    tryAgainButton->handleTouchesBegan(touches, pEvent);
    mainMenuButton->handleTouchesBegan(touches, pEvent);
}

void ResultScene::ccTouchesMoved(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    tryAgainButton->handleTouchesMoved(touches, pEvent);
    mainMenuButton->handleTouchesMoved(touches, pEvent);
}

void ResultScene::ccTouchesEnded(cocos2d::CCSet *touches, cocos2d::CCEvent *pEvent){
    tryAgainButton->handleTouchesEnded(touches, pEvent);
    mainMenuButton->handleTouchesEnded(touches, pEvent);
}